package com.demo2do.fms.admin.web.controller.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.export.DriverDistanceReportSearch;
import com.demo2do.fms.admin.entity.report.export.ReimbursementReportSearch;
import com.demo2do.fms.admin.entity.report.export.TruckDistanceReportSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.authority.OrganizationService;
import com.demo2do.fms.admin.service.report.ExportService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;

/**
 * 数据导出
 * 
 * @author sanz
 *
 */
@Controller
@RequestMapping("/exports")
@MenuSetting("menu-report")
public class ExportController {
    
    private static final Logger logger = LoggerFactory.getLogger(ExportController.class);

    @Autowired
    private ExportService exportService;
    
    @Autowired
    private OrganizationService organizationService;

    /**
     * 派车单月度报销信息统计 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/reimbursement-search", method = RequestMethod.GET)
    @MenuSetting("submenu-monthly-reimbursement")
    public ModelAndView reimbursementSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/export/reimbursement-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 派车单月度报销信息统计 - search
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "reimbursement")
    @MenuSetting("submenu-monthly-reimbursement")
    public ModelAndView onReimbursementSearch(ReimbursementReportSearch search, HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/export/reimbursement-list");
        
        int totals = exportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> result = exportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 派车单月度报销信息统计 - 导出所有记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/reimbursement-export", method = RequestMethod.GET)
    public ModelAndView reimbursementExport(ReimbursementReportSearch search, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/reimbursement-export.xlsx"));
        
        // 1. list driver distance
        List<Map<String, Object>> results = exportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        Organization organization = organizationService.load(Organization.class, search.getOrganizationId());
        modelAndView.addObject("organization", organization.getName());
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        logger.warn("#reimbursementExport() - export size=[{}]...", results.size());
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员月度行驶公里数信息统计 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/driver-distance-search", method = RequestMethod.GET)
    @MenuSetting("submenu-monthly-driver-distance")
    public ModelAndView driverDistanceSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/export/driver-distance-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员月度行驶公里数信息统计 - search
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "driver-distance")
    @MenuSetting("submenu-monthly-driver-distance")
    public ModelAndView onDriverDistanceSearch(DriverDistanceReportSearch search, HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/export/driver-distance-list");
        
        int totals = exportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> result = exportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员月度行驶公里数信息统计 - 导出所有记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/driver-distance-export", method = RequestMethod.GET)
    public ModelAndView driverDistanceExport(DriverDistanceReportSearch search, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/driver-distance-export.xlsx"));
        
        // 1. list driver distance
        List<Map<String, Object>> results = exportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        Organization organization = organizationService.load(Organization.class, search.getOrganizationId());
        modelAndView.addObject("organization", organization.getName());
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        logger.warn("#driverDistanceExport() - export size=[{}]...", results.size());
        
        return modelAndView;
        
    }
    
    /**
     * 驳运车月度行驶公里数信息统计 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/truck-distance-search", method = RequestMethod.GET)
    @MenuSetting("submenu-monthly-truck-distance")
    public ModelAndView truckDistanceSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/export/truck-distance-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 驳运车月度行驶公里数信息统计 - search
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "truck-distance")
    @MenuSetting("submenu-monthly-truck-distance")
    public ModelAndView onTruckDistanceSearch(TruckDistanceReportSearch search, HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/export/truck-distance-list");
        
        int totals = exportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> result = exportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 驳运车月度行驶公里数信息统计 - 导出所有记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/truck-distance-export", method = RequestMethod.GET)
    public ModelAndView truckDistanceExport(TruckDistanceReportSearch search, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/truck-distance-export.xlsx"));
        
        // 1. list driver distance
        List<Map<String, Object>> results = exportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        Organization organization = organizationService.load(Organization.class, search.getOrganizationId());
        modelAndView.addObject("organization", organization.getName());
        
        // 3. format utils
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        logger.warn("#truckDistanceExport() - export size=[{}]...", results.size());
        
        return modelAndView;
        
    }
    
}
